package generator;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;

public class ImgUtil {

    public static void main(String[] args) {
    	//commpressPicForScale("E:\\home\\bing.jpg", "E:\\home\\bing2.jpg", 100, 0.8);
    	File f = new File("E:\\home\\bing.jpg");
    	//System.out.println(imageCompression(f,1000,0.9));
    	/*
        try {
			Thumbnails.of("E:\\home\\bing.jpg")
			.scale(1f)
			.outputQuality(0.5f)
			.toFile("E:\\home\\bing2.jpg");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
    }
    
    public static InputStream imageCompression(InputStream is,Integer size,double quality) {
    	long fsize = 0;
    	if(size>fsize) {
    		return is;
    	}
    	try {
    		//InputStream nis = new FileInputStream(null);
    		File toFile = new File("E:\\home\\bg1.jpg"); 
 			Thumbnails.of(is)
 			.scale(1F)
 			.outputQuality(quality)
 			.toFile(toFile);
 			return null;//imageCompression(toFile,size, quality/2);
 		} catch (IOException e) {
 			return null;
 		}
    }
    
    static File imageCompression2(File file,Integer size) {
    	long fsize = file.length()/1024;
    	if(size>=fsize) {
    		return file;
    	}
    	try {
    		File toFile = new File("E:\\home\\bing3.jpg"); 
 			Thumbnails.of(file)
 			.scale(1F)
 			.outputQuality(1F)
 			.toFile(toFile);
 			return toFile;
 		} catch (IOException e) {
 			return null;
 		}
    }
    
    /**
	 * 
	 * @param srcPath  原图片地址
	 * @param desPath  目标图片地址
	 * @param desFileSize  指定图片大小,单位kb
	 * @param accuracy 精度,递归压缩的比率,建议小于0.9
	 * @return
	 */
	public static String commpressPicForScale(String srcPath,String desPath,
			long desFileSize , double accuracy){
		try {
			File srcFile  = new File(srcPath);
			long srcFilesize = srcFile.length();
			System.out.println("原图片:"+srcPath + ",大小:" + srcFilesize/1024 + "kb");
			//递归压缩,直到目标文件大小小于desFileSize
			commpressPicCycle(desPath, desFileSize, accuracy);
			
			File desFile = new File(desPath);
			System.out.println("目标图片:" + desPath + ",大小" + desFile.length()/1024 + "kb");
			System.out.println("图片压缩完成!");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return desPath;
	}

	public static void commpressPicCycle(String desPath , long desFileSize,
			double accuracy) throws IOException{
		File imgFile = new File(desPath);
		long fileSize  = imgFile.length();
		//判断大小,如果小于500k,不压缩,如果大于等于500k,压缩
		if(fileSize <= desFileSize * 500){
			return;
		}
		//计算宽高
		BufferedImage  bim = ImageIO.read(imgFile);
		int imgWidth = bim.getWidth();
		int imgHeight = bim.getHeight();
		int desWidth = new BigDecimal(imgWidth).multiply(
                new BigDecimal(accuracy)).intValue();
        int desHeight = new BigDecimal(imgHeight).multiply(
                new BigDecimal(accuracy)).intValue();
        Thumbnails.of(desPath).size(desWidth, desHeight).outputQuality(accuracy).toFile(desPath);
        //如果不满足要求,递归直至满足小于1M的要求
        commpressPicCycle(desPath, desFileSize, accuracy);
	}
    
    
}
